<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <el-row style="height: 40px">
          <el-col :span="20">{{ company.companyName }}</el-col>
          <el-col :span="4">
            <el-row>
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <el-dropdown>
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepts(company)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- data:表示让el-tree帮你渲染的数据 -->
        <!-- node-click:点击el-tree的每一个节点时触发 -->
        <!-- default-expand-all:默认展开所有节点 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row style="width: 100%;">
              <el-col :span="20">{{ data.name }}</el-col>
              <el-col :span="4">
                <el-row>
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- element-ui中，如果要传默认参数 应该使用 $event -->
                    <el-dropdown @command="handleCommand($event,data)">
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                        <el-dropdown-item command="edit">修改部门</el-dropdown-item>
                        <!-- @click.native="clickHandle(data.id)" -->
                        <el-dropdown-item command="del">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- .sync修饰符：表示父组件把传过去的值的修改权 交给组件，让子组件去修改传过去的props值 -->
      <!--  @closeDialgEmit="closeDialogFn" -->
      <addDept ref="addDept" :show-dialog.sync="showAddDeptDialog" :node-data="nodeData" :depart-list="departList" />
    </div>
  </div>
</template>

<script>
import { getDepartmentListApi, delDepartmentApi } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import addDept from './components/add-dept'
export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      // label 是显示当前节点的文本
      // children 是控制当前节点有没子级
      departs: [],
      company: {
        companyName: '',
        id: '',
        children: []
      },
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showAddDeptDialog: false,
      // 点击某个部门时 使用的变量
      nodeData: {},
      // 组织架构的源数据
      departList: [],
      isLoading: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    handleCommand(cmd, data) {
      if (cmd === 'add') {
        this.addDepts(data)
      }
      if (cmd === 'edit') {
        this.editEdpts(data)
      }
      if (cmd === 'del') {
        this.delDepts(data)
      }
    },
    /**
     * 编辑部门
     */
    editEdpts(data) {
      // DOM更新不完毕，子组件接受不到父组件传过去的值
      this.nodeData = data
      this.showAddDeptDialog = true
      this.$nextTick(() => {
        // 表示DOM更新完毕了
        this.$refs.addDept.getDepartmentDetail()
      })
    },
    /**
     * 添加部门
     */
    addDepts(data) {
      this.nodeData = data
      this.showAddDeptDialog = true
    },
    /**
     * 关闭弹框
     */
    closeDialogFn() {
      this.showAddDeptDialog = false
    },
    /**
     * 删除部门
     */
    delDepts(data) {
      this.$confirm('亲！是否要删除该部门？此操作不可逆', '温馨提示').then(async() => {
        // 当用户点击确定时候会执行then
        await delDepartmentApi(data.id)
        this.$message.success('恭喜！删除成功')
        this.getDepartmentList()
      }).catch(() => {
        // 当用户点击取消的时候会执行catch
      })
    },
    /**
     * 获取部门列表
     */
    async getDepartmentList() {
      this.isLoading = true
      const { data } = await getDepartmentListApi()
      this.company.companyName = data.companyName
      this.departList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      this.company.children = this.departs
      this.isLoading = false
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
